//package com.xdcplus.web.common.plugins;
//
//import cn.hutool.core.util.StrUtil;
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.annotation.TableName;
//import com.xdcplus.web.common.pojo.entity.crack.TpCrackBsgTest;
//import com.xdcplus.web.common.pojo.entity.crack.TpCrackPsgTest;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.binding.MapperMethod;
//import org.apache.ibatis.executor.Executor;
//import org.apache.ibatis.mapping.MappedStatement;
//import org.apache.ibatis.mapping.SqlCommandType;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.plugin.Intercepts;
//import org.apache.ibatis.plugin.Invocation;
//import org.apache.ibatis.plugin.Signature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.kafka.core.KafkaTemplate;
//import org.springframework.stereotype.Component;
//
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@Component
//@Intercepts(
//        {
//                @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
//        }
//)
//@Slf4j
//public class AsyncDmlPlugin implements Interceptor {
//    @Autowired
//    private KafkaTemplate<String, String> kafkaTemplate;
//    private String MES_TABLE_DML_HIS_TOPIC = "tp_crack_test_to_doris";
//    /**
//     * 拦截方法逻辑
//     */
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//        // 获取 Executor 对象
////        Executor executor = (Executor) invocation.getArgs()[0];
//
//        Object target = invocation.getTarget();
//        Object[] args = invocation.getArgs();
//        if (target instanceof Executor) {
//            final Executor executor = (Executor) target;
////            Transaction transaction = executor.getTransaction();
//            try {
//                Object parameter = args[1];
//                boolean isUpdate = args.length == 2;
//                MappedStatement ms = (MappedStatement) args[0];
//                if (isUpdate && (ms.getSqlCommandType() == SqlCommandType.UPDATE || ms.getSqlCommandType() == SqlCommandType.INSERT ||  ms.getSqlCommandType() == SqlCommandType.DELETE) && parameter != null) {
//                    Object result;
//                    if (ms.getSqlCommandType() == SqlCommandType.UPDATE) {
//                        result = ((MapperMethod.ParamMap) parameter).get("et");
//                    }else if (ms.getSqlCommandType() == SqlCommandType.DELETE) {
//                        result = ((MapperMethod.ParamMap) parameter).get("et");
//                    } else {
//                        result = parameter;
//                    }
//                    if (result instanceof TpCrackBsgTest || result instanceof TpCrackPsgTest) {
//                        TableName tableName = result.getClass().getAnnotation(TableName.class);
//                        if (tableName != null) {
//                            pushMq(ms, result, tableName);
//                        }
//                    }
//                }
//            } catch (Exception e) {
//                // 如果出现异常，可能需要回滚事务
////                transaction.rollback();
//                throw e;
//            } finally {
//                // 最后，提交或回滚事务
////                transaction.commit();
//            }
//        }
//        return invocation.proceed();
//    }
//
//    private void pushMq(MappedStatement ms, Object result, TableName tableName) {
//        OggMessageDto oggMessageDto = new OggMessageDto();
//        oggMessageDto.setTable(tableName.value());
//        Map<String, Object> objectMap = JSON.parseObject(JSON.toJSONString(result)).getInnerMap()
//                .entrySet()
//                .stream()
//                .collect(Collectors.toMap(key -> StrUtil.toUnderlineCase(key.getKey()).toUpperCase(), Map.Entry::getValue));
//        if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
//            oggMessageDto.setOpType("I");
//            oggMessageDto.setAfter(objectMap);
//        } else if (ms.getSqlCommandType() == SqlCommandType.DELETE) {
//            oggMessageDto.setOpType("D");
//            oggMessageDto.setBefore(objectMap);
//        } else {
//            oggMessageDto.setOpType("U");
//            oggMessageDto.setAfter(objectMap);
//        }
//        // 获取当前的日期和时间
//        LocalDateTime now = LocalDateTime.now();
//        // 定义日期时间格式
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
//        // 格式化日期和时间
//        String formattedDateTime = now.format(formatter);
//        oggMessageDto.setCurrentTs(formattedDateTime);
//        oggMessageDto.setOpTs(formattedDateTime);
//        kafkaTemplate.send(MES_TABLE_DML_HIS_TOPIC, JSON.toJSONString(oggMessageDto));
//    }
//
//
//}
